﻿Imports VN.INS.BL.BC
Imports System.Data

Partial Class Devida_PAC
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not (Page.IsPostBack)) Then

            Dim g_strPPER As String
            g_strPPER = Session("PPER").ToString
            ListaPeriodo()
            If ViewState("COD_PERIODO") <> "" Then
                cargar_grilla(g_strPPER, ddlPeriodo.SelectedValue.ToString)
            End If
            ViewState("PPERUSU") = g_strPPER
        End If
    End Sub

    'cargamos grilla pac
    Private Sub cargar_grilla(ByVal pper As String, ByVal PERIODO As String)
        Dim objpacbc As PacBC = New PacBC
        Dim dtpac As DataTable
        dtpac = objpacbc.cargar_pac(pper, PERIODO)
        ViewState("PPER") = dtpac.Rows(0).Item("ABREVIATURA")
        gv_pac.DataSource = dtpac
        gv_pac.DataBind()
    End Sub

    Protected Sub gv_pac_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv_pac.RowDataBound
        If e.Row.RowIndex >= 0 Then
            'damos formato a las filas
            Select Case e.Row.Cells(17).Text
                Case 1
                    e.Row.Cells(17).BackColor = Drawing.Color.FromArgb(50, 150, 100)
                Case 2
                    e.Row.Cells(17).BackColor = Drawing.Color.FromArgb(250, 150, 0)
                Case 3
                    e.Row.Cells(17).BackColor = Drawing.Color.FromArgb(250, 50, 50)
                Case 4
                    e.Row.Cells(17).BackColor = Drawing.Color.White
            End Select
            e.Row.Cells(17).Text = ""
            If e.Row.Cells(18).Text = "&nbsp;" Then
                e.Row.Cells(18).Text = "Falta ingresar"
                e.Row.Cells(18).ForeColor = Drawing.Color.Red
            Else
                e.Row.Cells(18).Text = "Ingresado"
            End If
        End If
        If e.Row.RowType = DataControlRowType.DataRow Then
            ' ASIGNA EVENTOS
            e.Row.Attributes.Add("OnMouseOver", "Resaltar_On(this);")
            e.Row.Attributes.Add("OnMouseOut", "Resaltar_Off(this);")
            e.Row.Attributes("OnClick") = Page.ClientScript.GetPostBackClientHyperlink(Me.gv_pac, "Select$" + e.Row.RowIndex.ToString)
        End If
    End Sub

    Protected Sub gv_pac_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gv_pac.SelectedIndexChanged
        'capturamos los parametros de la fila seleccionada
        Dim strNombre As String = Server.HtmlDecode(UCase(gv_pac.SelectedRow.Cells(1).Text.ToString))
        ViewState("Nombre") = strNombre
        Dim HDF_IDPAC As HiddenField = CType(gv_pac.SelectedRow.FindControl("ID_PAC"), HiddenField)
        Dim HDF_SEG_PAC As HiddenField = CType(gv_pac.SelectedRow.FindControl("SEG_PAC"), HiddenField)
        Dim HDF_SEM As HiddenField = CType(gv_pac.SelectedRow.FindControl("SEM"), HiddenField)
        ViewState("ID_PAC") = HDF_IDPAC.Value.ToString
        ViewState("SEG_PAC") = HDF_SEG_PAC.Value.ToString
        ViewState("SEM") = HDF_SEM.Value.ToString
        Dim CONDIC As String = Server.HtmlDecode(gv_pac.SelectedRow.Cells(18).Text.ToString)
        Dim ACCION As String = Server.HtmlDecode(gv_pac.SelectedRow.Cells(19).Text.ToString)
        Dim obser As String = Server.HtmlDecode(gv_pac.SelectedRow.Cells(20).Text.ToString)
        ViewState("CONDICION") = CONDIC
        ViewState("ACCION") = ACCION
        ViewState("OBSERV") = obser
    End Sub

    Protected Sub btnAgregar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnAgregar.Click
        'redirecciona nuevo si avance pac aun no ha sido ingresado y si el periodo pac está activo
        If gv_pac.SelectedIndex > -1 Then
            If ViewState("CONDICION").ToString = "Ingresado" Then
                alerta.Mostrar_Alerta("Aviso", "El Avance del PAC ya fué ingresado")
            Else
                Try
                    Dim objGeneral As GeneralBC
                    objGeneral = New GeneralBC
                    ViewState("Estado") = objGeneral.EstadoFechaPAC(ddlPeriodo.SelectedValue.ToString)
                Catch ex As Exception
                    Throw ex
                End Try

                Dim prd As String = Now.Month.ToString + "-" + Now.Year.ToString
                If ViewState("filas") > 0 Then
                    If ViewState("Estado") = "1" Then
                        Response.Redirect("Avance_pac.aspx?PPER=" + ViewState("PPER").ToString & "&ID_PAC=" & ViewState("ID_PAC").ToString & "&PERIODO2=" & ViewState("COD_PERIODO").ToString & "&PERIODO=" & ddlPeriodo.SelectedItem.Text & "&Nombre=" & ViewState("Nombre").ToString & "&Nuevo=N")

                    Else
                        alerta.Mostrar_Alerta("Alerta", "El Periodo no corresponde al Mes Vigente")
                        Exit Sub
                    End If
                Else
                    alerta.Mostrar_Alerta("Alerta", "No existe Periodo")
                End If
            End If
        Else
            alerta.Mostrar_Alerta("Aviso", "Debe seleccionar un registro")
        End If
    End Sub

    Public Sub ListaPeriodo()
        Dim objPacBC As PacBC
        Dim objData As DataTable
        objPacBC = New PacBC
        objData = New DataTable
        objData = objPacBC.ListaPeriodoPAC(Session("Año"))
        ViewState("filas") = objData.Rows.Count
        ViewState("perd_PAC") = objData
        ddlPeriodo.DataSource = objData
        ddlPeriodo.DataTextField = "DESCRIPCION"
        ddlPeriodo.DataValueField = "PERIODO"
        ddlPeriodo.DataBind()
        ViewState("COD_PERIODO") = ddlPeriodo.SelectedValue
    End Sub

    Protected Sub ddlPeriodo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlPeriodo.SelectedIndexChanged
        'cargamos grilla segun periodo seleccionado
        ViewState("COD_PERIODO") = ddlPeriodo.SelectedValue
        cargar_grilla(ViewState("PPERUSU").ToString, ddlPeriodo.SelectedValue.ToString)
        Dim seg_pac As New DataTable
        seg_pac = ViewState("perd_PAC")
        Dim A As String = seg_pac.Rows(ddlPeriodo.SelectedIndex).Item("ESTADO").ToString
        gv_pac.SelectedIndex = -1
    End Sub

    Protected Sub btnModificar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnModificar.Click
        'redirecciona a edicion si avance pac ya fué ingresado y si el periodo pac está activo
        If gv_pac.SelectedIndex > -1 Then
            If ViewState("CONDICION").ToString <> "Ingresado" Then
                alerta.Mostrar_Alerta("Aviso", "El Avance del PAC no fué ingresado")
            Else
                Try
                    Dim objGeneral As GeneralBC
                    objGeneral = New GeneralBC
                    ViewState("Estado") = objGeneral.EstadoFechaPAC(ddlPeriodo.SelectedValue.ToString)
                Catch ex As Exception
                    Throw ex
                End Try
                If ViewState("Estado") = "1" Then
                    Response.Redirect("Avance_pac.aspx?PPER=" + ViewState("PPER").ToString & "&ACCION=" & ViewState("ACCION").ToString & "&OBSERV=" & ViewState("OBSERV").ToString & "&SEG_PAC=" & ViewState("SEG_PAC").ToString & "&SEM=" & ViewState("SEM").ToString & "&ID_PAC=" & ViewState("ID_PAC").ToString & "&PERIODO2=" & ViewState("COD_PERIODO").ToString & "&PERIODO=" & ddlPeriodo.SelectedItem.Text & "&Nombre=" & ViewState("Nombre") & "&Nuevo=M")
                Else
                    alerta.Mostrar_Alerta("Aviso", "Solo se puede ingresar datos del periodo actual")
                End If
            End If
        Else
            alerta.Mostrar_Alerta("Aviso", "Debe seleccionar un registro")
        End If
    End Sub

End Class
